import 'package:flutter/material.dart';
import 'package:flutter_cookbook/entity/MainNav.dart';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_cookbook/ui/widgets/CookbookMenuWidget.dart';
import 'package:flutter_cookbook/ui/widgets/HomeSearchWidget.dart';
import 'package:flutter_cookbook/ui/widgets/MyUserWidget.dart';

class HomePage extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Main(),
    );
  }
  
}

class Main extends StatefulWidget {
  
  @override
  State<StatefulWidget> createState() {
    return _Main();
  }
  
}

class _Main extends State {
  
  var index = 0;

  var pages = [MainNav("首页", HomeWidget()), MainNav("菜单", MenuWidget()), MainNav("用户", UserWidget())];
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: pages[index].widget,
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: pages[0].label),
          BottomNavigationBarItem(icon: Icon(Icons.menu), label: pages[1].label),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: pages[2].label),
        ],
        onTap: (int) {
          print("index -> $int");

          navSelect(int);
        },
        currentIndex: index,
      ),
      appBar: AppBar(
        brightness: Brightness.dark,
        title: Text(pages[index].label),
        centerTitle: true,
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }
  
  void navSelect(var int) {
    
    if (index != int) {
      setState(() {
        index = int;
      });
    }
  }
  
}

class HomeWidget extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return HomeSearchWidget();
  }
  
}

class MenuWidget extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return CookbookMenuWidget();
  }
  
}

class UserWidget extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return MyUserWidget();
  }
  
}